<?php
namespace Hilaoyu\LaravelExtend\Supports;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Relations\Relation as Relation;


/**
 * Created by laoyu.
 * User: hilaoyu@qq.com
 * Date: 2019/4/9
 * Time: 17:28
 */



class IlluminateQuery
{
    /**
     * @param $query
     * @return Model|EloquentBuilder|QueryBuilder|Relation
     */
    static public function check($query)
    {
        if (($query instanceof Model)
            || ($query instanceof EloquentBuilder)
            || ($query instanceof QueryBuilder)
            || ($query instanceof Relation)
        ) {
            return $query;

        }

        return null;
    }

    /**
     * @param $query
     * @param array $where
     * @return Model|EloquentBuilder|QueryBuilder|Relation
     */
    static public function buildWhere($query,$where=[]){

        if(! static::check($query)){
            return $query;
        }

        if(!empty($where)){
            if(is_callable($where)){
                $query = call_user_func($where,$query);
            }else{
                $query = $query->where($where);
            }
        }

        return $query;
    }
}